package com.sumainfor.zuul.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.sumainfor.common.annotation.SysLog;
import com.sumainfor.common.util.Constant;
import com.sumainfor.common.util.FieldCheckUtil;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.common.util.R;
import com.sumainfor.zuul.entity.DeptMenu;
import com.sumainfor.zuul.entity.SysDeptEntity;
import com.sumainfor.zuul.service.SysDeptService;
import com.sumainfor.zuul.service.impl.Feign.FeignDeptService;
import org.apache.commons.collections.MapUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 部门管理
 */
@RestController
@RequestMapping("/sys/dept")
public class SysDeptController extends AbstractController {

    @Autowired
    private SysDeptService sysDeptService;

    @Autowired
    FeignDeptService FeignDeptService;


    /**
     * 获取所有的部门及上级部门
     *
     * @author zhlu
     * @date 2018年9月20日
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:dept:list")
    public JsonResult list(@RequestParam Map<String, Object> params) {
        List<Map<String, Object>> data = sysDeptService.getDetpList(params);
        Integer size = MapUtils.getInteger(params, "pagesize");
        Integer page = MapUtils.getInteger(params, "pageno");


        if (size != null && page != null) {
            if (page < 0) {
                page = 1;
            }
            if (size < 0) {
                size = 10;
            }
            Page pageData = new Page(page, size);
            pageData.setTotal(data.size());
            pageData.setRecords(data.stream().skip((page - 1) * size).limit(size).collect(Collectors.toList()));
            return JsonResult.success(pageData);
        }

        return JsonResult.success(data);
    }

    /**
     * 选择部门(添加、修改菜单)
     */
    @RequestMapping("/select")
    @RequiresPermissions("sys:dept:select")
    public R select() {
        List<SysDeptEntity> deptList = sysDeptService.queryList(new HashMap<String, Object>());

        //添加一级部门
        if (getUserId() == Constant.SUPER_ADMIN) {
            SysDeptEntity root = new SysDeptEntity();
            root.setDeptId(0);
            root.setName("一级部门");
            root.setParentId(-1);
            root.setOpen(true);
            deptList.add(root);
        }

        return R.ok().put("deptList", deptList);
    }

    /**
     * 根据编号获取部门
     *
     * @Description: TODO(这里用一句话描述这个方法的作用)
     * @author zhlu
     * @date 2018年9月21日
     */
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    @RequiresPermissions("sys:dept:info")
    public JsonResult getDetpMap(@RequestParam Map<String, Object> params) {
        JsonResult result = new JsonResult();
        return result.put(sysDeptService.getDetpMap(params));
    }

    /**
     * 保存
     */
    @SysLog(value = "新增部门")
    @RequestMapping("/save")
    @RequiresPermissions("sys:dept:save")
    public R save(@RequestBody SysDeptEntity dept) {
        dept.setDeptId(null);
        if (dept.getAreaList() == null || dept.getAreaList().isEmpty()) {
            return R.error("参数不完整");
        }
        boolean state = sysDeptService.createDept(dept);
        return state ? R.ok() : R.error("操作失败");
    }

    /**
     * 修改
     */
    @SysLog(value = "修改部门")
    @RequestMapping("/update")
    @RequiresPermissions("sys:dept:update")
    public R update(@RequestBody SysDeptEntity dept) {
        if (dept == null || dept.getDeptId() == null) {
            return R.error("参数不完整");
        }

        if (dept.getAreaList() == null || dept.getAreaList().isEmpty()) {
            return R.error("参数不完整");
        }
        boolean state = sysDeptService.updateDept(dept);
        return state ? R.ok() : R.error("操作失败");
    }


    /**
     * 删除
     */
    @SysLog(value = "删除部门")
    @RequestMapping("/delete")
    @RequiresPermissions("sys:dept:delete")
    public JsonResult delete(@RequestBody List<Integer> ids) {
        if (ids == null || ids.isEmpty()) {
            return JsonResult.actionFailure();
        }
        return sysDeptService.deleteDeptById(ids);
    }

    /**
     * 所有部门
     */
    @RequestMapping("/getDeptList")
    @RequiresPermissions("sys:dept:list")
    public JsonResult getDeptList(@RequestParam(value = "isShowTop", required = false, defaultValue = "true") Boolean isShowTop
            , @RequestParam(value = "areaId", required = false) String areaId) {
        List<DeptMenu> tree = sysDeptService.findTree(isShowTop, areaId);
        return JsonResult.success(tree);
    }

    /**
     * 功能描述:
     * 获取所有的服务中心
     *
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/5/9 17:57
     */
    @GetMapping("/getServiceDeptList")
    public JsonResult getServiceDeptList() {
        JsonResult result = new JsonResult();
        return result.put(sysDeptService.getServiceDeptList());
    }

    /**
     * 功能描述:
     * 根据公司集合获取所有服务中心的列表
     *
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/6/20 18:57
     */
    @PostMapping("/getAreaByDetLists")
    public JsonResult getAreaByDetLists(@RequestBody Map<String, Object> params) {
        if (FieldCheckUtil.isMapFieldNotNull(params, new String[]{"areaList"})) {
            List<String> areaList = (List<String>) params.get("areaList");
            if (areaList.size() == 0) {
                return JsonResult.error("areaList不能为空！");
            }
        }
        return JsonResult.success(FeignDeptService.getAreaByDetLists(params));
    }
}
